﻿

using System;
using AutoMapper;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using System.Collections.Generic;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Authorization;

using Oliving.Edge.AuthHelper;
using Oliving.Edge.Model;
using Oliving.Edge.Model.Models;
using Oliving.Edge.IServices;
using Oliving.Edge.Model.Dto;
using System.Linq.Expressions;
using Oliving.Edge.Common.Extensions;

namespace Oliving.Edge.Controllers
{
   
    [ApiController]
    [Route("api/DTSN_Canning")]
    [Authorize(Permissions.Name)]
    public class DTSN_CanningController : OlivingControll
    {
        IMapper _mapper;
        private readonly IDTSN_CanningServices _DTSN_CanningServices;
        private readonly ILogger<DTSN_CanningController> _logger;


        public DTSN_CanningController(
                IHttpContextAccessor accessor, PermissionRequirement requirement,
                IMapper mapper, ILogger<DTSN_CanningController> logger,
                IDTSN_CanningServices DTSN_CanningServices
            ) : base(accessor, requirement)
        {
            _logger = logger;
            _mapper = mapper;
            _DTSN_CanningServices = DTSN_CanningServices;
        }



        [HttpGet]
        [AllowAnonymous]
        [Route("/api/[controller]/Get")]
        public async Task<MessageModel<DTSN_CanningDto>> Get(int id)
        {
            var data = new MessageModel<DTSN_CanningDto>();
            DTSN_Canning model = await _DTSN_CanningServices.QueryById(id);
            if (model == null)
            {
                data.response = null;
                data.msg = "数据不存在！";
                data.success = false;
            }
            else
            {
                var dto = _mapper.Map<DTSN_CanningDto>(model);
                data.response = dto;
                data.msg = "获取成功！";
                data.success = true;
            }
            return data;
        }





        [HttpGet]
        [Route("/api/[controller]/GetPageList")]
        public async Task<MessageModel<PageModel<DTSN_CanningPageDto>>> GetPageList(
            int page = 1, int PageSize = 20,
            string sidx = "", string order = "",
            string key = "")
        {
            Expression<Func<DTSN_Canning, bool>> whereExpression = c => true && (c.Canning_State != -1);

            if (string.IsNullOrEmpty(key) || string.IsNullOrWhiteSpace(key))
            {
                key = "";
            }
            else
            {
                whereExpression = whereExpression.And(c =>
                        c.Canning_Car.Contains(key)
                        || c.Canning_CarCode.Contains(key)
                        || c.Canning_Customer.Contains(key)
                        || c.Canning_Order.Contains(key)
                    );
            }

            string colOrder = "";
            if (!string.IsNullOrEmpty(sidx))
            {
                colOrder = sidx + " " + order;
            }
            var data = await _DTSN_CanningServices.QueryPageList(whereExpression, page, PageSize, colOrder);

            #region MyRegion

            //var data = await _DTSN_CanningServices.QueryPage(a =>
            //                true
            //                , page, intPageSize, " Canning_ID desc "); 
            //var model = _mapper.Map<PageModel<DTSN_CanningDto>>(data);

            #endregion

            return new MessageModel<PageModel<DTSN_CanningPageDto>>()
            {
                msg = "获取成功",
                success = true,
                response = data
            };
        }



        [HttpPut]
        [Route("/api/[controller]/Update")]
        public async Task<MessageModel<string>> Update([FromBody] DTSN_CanningDto dto)
        {
            var data = new MessageModel<string>();

            if (dto != null && dto.Canning_ID > 0)
            {
                DTSN_Canning model = await _DTSN_CanningServices.QueryById(dto.Canning_ID);
                if (model != null)
                {
                    model.Canning_Car = dto.Canning_Car;
                    model.Canning_CarCode = dto.Canning_CarCode;
                    model.Canning_Customer = dto.Canning_Customer;
                    model.Canning_TongDaoID = dto.Canning_TongDaoID;
                    model.Canning_Tare = dto.Canning_Tare;
                    model.Canning_GrossWeight = dto.Canning_GrossWeight;
                    model.Canning_NetWeight = dto.Canning_NetWeight;
                    model.Canning_Varieties = dto.Canning_Varieties;
                    model.Canning_BeginTime = dto.Canning_BeginTime;
                    model.Canning_EndTime = dto.Canning_EndTime;
                    model.Canning_Count = dto.Canning_Count;
                    model.Canning_State = dto.Canning_State;
                    model.Canning_CreateTime = dto.Canning_CreateTime;
                    model.Canning_InsertTime = dto.Canning_InsertTime;
                    model.Canning_Remark = dto.Canning_Remark;
                    model.Canning_Order = dto.Canning_Order;
                    model.Canning_weightserialcode = dto.Canning_weightserialcode;
                    model.Canning_accountcode = dto.Canning_accountcode;
                    model.Canning_accountname = dto.Canning_accountname;
                    model.Canning_carrycode = dto.Canning_carrycode;
                    model.Canning_carryname = dto.Canning_carryname;
                    model.Canning_procode = dto.Canning_procode;
                    model.Canning_proname = dto.Canning_proname;
                    model.Canning_trucktype = dto.Canning_trucktype;
                    model.Canning_truckweight = dto.Canning_truckweight;
                    model.Canning_storage = dto.Canning_storage;
                    model.Canning_OrderCreatetime = dto.Canning_OrderCreatetime;
                    model.Canning_Orderbstype = dto.Canning_Orderbstype;
                    model.Canning_Orderpreweight = dto.Canning_Orderpreweight;
                    model.Canning_Ordertareweight = dto.Canning_Ordertareweight;
                    model.Canning_Ordergrossweight = dto.Canning_Ordergrossweight;
                    model.Canning_Ordernetweight = dto.Canning_Ordernetweight;
                    model.Canning_Orderstatus = dto.Canning_Orderstatus;


                    data.success = await _DTSN_CanningServices.Update(model);

                    if (data.success)
                    {
                        data.msg = "更新成功";
                        data.response = dto.Canning_ID.ObjToString();
                    }
                }
                else
                {
                    data.success = false;
                    data.msg = "参数异常，您要更新的数据不存在";
                }
            }
            else
            {
                data.success = false;
                data.msg = "参数异常";
            }
            return data;
        }





        [HttpPost]
        [Route("/api/[controller]/Add")]
        public async Task<MessageModel<string>> Add(DTSN_Canning dto)
        {
            var data = new MessageModel<string>();
            int id = await _DTSN_CanningServices.Add(dto);

            data.success = id > 0;
            if (data.success)
            {
                data.response = id.ObjToString();
                data.msg = "添加成功";
            }
            return data;
        }




        [HttpDelete]
        [Route("/api/[controller]/Delete")]
        public async Task<MessageModel<string>> Delete(int id)
        {
            var data = new MessageModel<string>();
            if (id > 0)
            {
                var userDetail = await _DTSN_CanningServices.QueryById(id);
                userDetail.Canning_State = -1;// ?????????????
                data.success = await _DTSN_CanningServices.Update(userDetail);
                if (data.success)
                {
                    data.msg = "删除成功";
                    data.response = userDetail?.Canning_ID.ObjToString();
                }
            }
            return data;
        }





        [HttpDelete]
        [Route("/api/[controller]/BatchDelete")]
        public async Task<MessageModel<string>> BatchDelete(string ids)
        {
            var data = new MessageModel<string>();
            if (!string.IsNullOrEmpty(ids))
            {
                if (false)
                {
                    #region 软删除

                    int[] list = Array.ConvertAll<string, int>(ids.Split(','), s => int.Parse(s));

                    DTSN_Canning model = new DTSN_Canning();
                    //  model.C_State = -1;          ???????????????????
                    string whereStr = " Canning_ID in (" + ids + ")";
                    List<string> colList = new List<string>();
                    //  colList.Add("C_State");    ???????????????????
                    List<string> colHL = new List<string>();

                    bool isOK = await _DTSN_CanningServices.Update(model, colList, colHL, whereStr);

                    data.success = isOK;
                    if (data.success)
                    {
                        data.msg = "删除成功";
                        data.response = list.Count().ObjToString();
                    }

                    #endregion
                }
                {
                    #region 直接删除 

                    object[] list = Array.ConvertAll<string, object>(ids.Split(','), s => int.Parse(s));

                    bool isOK = await _DTSN_CanningServices.DeleteByIds(list);

                    data.success = isOK;
                    if (data.success)
                    {
                        data.msg = "删除成功";
                        data.response = list.Count().ObjToString();
                    }
                    #endregion
                }
            }
            return data;
        }




    }
}

